<?php
session_start();

include_once( './data/defines.php' );

if(!isset($_SESSION[SESS_ACCESS_TOKEN])) {
    include_once( './data/weibo.config.php' );
    include_once( './api/saetv2.ex.class.php' );
    include_once( './api/api_user.php' );

    $o = new SaeTOAuthV2( WB_AKEY , WB_SKEY );

    if (isset($_REQUEST['code'])) {
        $keys = array();
        $keys['code'] = $_REQUEST['code'];
        $keys['redirect_uri'] = WB_CALLBACK_URL;
        try {
            $token = $o->getAccessToken( 'code', $keys ) ;
        } catch (OAuthException $e) {
            echo "Exception while getAccessToken : " . $e;
        }
    }

    if (isset($token)) {
        $_SESSION[SESS_ACCESS_TOKEN] = $token["access_token"];
        $_SESSION[SESS_WEIBO_USER_ID] = $token["uid"];
        setcookie( 'weibojs_'.$o->client_id, http_build_query($token) );
        $ret = user_login($token["uid"]);
        if($ret[FLAG_RET] == NO_ERR) {
            $_SESSION[SESS_USER_ID] = $ret[FLAG_USER_ID];
            //echo $_SESSION[SESS_WEIBO_USER_ID] . ", " . $_SESSION[SESS_USER_ID];
        } else {
            //...
        }
        
        while(list($key,$val)= each($token)) {
            echo "$key=> $val<br />";
        }
    } else {
        //若没有获取到access token，则发起授权请求
        Header("HTTP/1.1 303 See Other"); 
        Header("Location: index.php");
        exit;
    }
} else {
    //...
}

?>

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<title>人在旅途</title>
<!-- 样式 -->
<link rel="stylesheet" type="text/css" href="./style/style.css"/>
<!-- JQuery -->
<script type="text/javascript" src="http://ajax.microsoft.com/ajax/jquery/jquery-1.4.min.js"></script>
<!-- Google Map API -->
<script type="text/javascript" src="http://ditu.google.com/maps/api/js?key=AIzaSyDnl__I7zin8KlfOIyI9FmaEUCCJxWixCk&sensor=false"></script>
<!-- 外部API -->
<script type="text/javascript" src="./js/fancybox/jquery.mousewheel-3.0.4.pack.js"></script>
<script type="text/javascript" src="./js/fancybox/jquery.fancybox-1.3.4.pack.js"></script>
<link rel="stylesheet" type="text/css" href="./js/fancybox/jquery.fancybox-1.3.4.css" media="screen" />
<script type="text/javascript" src="./js/utils.js"></script>
<script type="text/javascript" src="./js/hashMap.js"></script>
<!-- 地图相关API -->
<script type="text/javascript" src="./js/map.js"></script>
<!-- 运行脚本 -->
<script type="text/javascript">
<?php
    echo "setAccessToken('" . $_SESSION[SESS_ACCESS_TOKEN] ."');\n";
    //判断网页类型
    $which_w = "DISPLAY_TYPE_NORMAL";
    $which = $_REQUEST["which"];
    if(isset($which)) {
        switch($which) {
            case "suggest":     //公共墙
                $which_w = "DISPLAY_TYPE_SUGGEST";
                break;
            
            default:
                break;
        }
    }
    echo 'var type = ' . $which_w;
?>

function init() {
    loadMap(document.getElementById("container"), type);
}
</script>
</head>

<body onload="init()">
<!-- 标题栏 -->
<div class="header">
    <div class="header_img"><a href="./callback.php"><img src="./style/images/logo.png"/></a></div>
    <div class="header_a"><a href="./callback.php">我的</a></div>
    <div class="header_a"><a href="./callback.php?which=suggest">公共墙</a></div>
</div>
<!-- 地图容器 -->
<div id="container"></div>
<!-- 居中加载元素 -->
<div id="loading_hint" class="middle_screen">
    <div id="loading_img"><img src="./style/images/loading.gif"/></div>
    <div id="loading_text">加载中</div>
</div>
</body>
</html>
